package com.wu.creditmanager.entity;

import java.io.Serializable;
import java.util.Calendar;

import android.content.Context;
import android.database.Cursor;

import com.wu.creditmanager.R;

public class Credit implements Serializable {
	private static final long serialVersionUID = -7920723852606711004L;
	public static final String TAG = "Credit";

	public static final String TBL_NAME = "tbl_credits";
	public static final String NAME = "name";
	public static final String BANK_ID = "bank_id";
	public static final String LAST4CODE = "last4code";
	public static final String STATEMENTDAY = "statementDay";
	public static final String PAYMENTDUEDATE = "paymentDueDate";
	public static final String RELATIVE = "relative";
	public static final String ICON = "icon";
	public static final String VALID_YEAR = "valid_year";
	public static final String VALID_MONTH = "valid_month";
	public static final String LIMIT_RMB = "RMBlimit";
	public static final String LIMIT_USD = "USDlimit";

	private long id;
	private String name;
	private Bank bank;
	private String last4code;
	private int statementDay;
	private int paymentDueDate;
	private int relative = 0;
	private String icon;
	private float limitRMB;
	private float limitUSD;
	private int validYear;
	private int validMonth;
	private Calendar nextAlarmTime = null;

	public int getValidYear() {
		return validYear;
	}

	public void setValidYear(int validYear) {
		this.validYear = validYear;
	}

	public int getValidMonth() {
		return validMonth;
	}

	public void setValidMonth(int validMonth) {
		this.validMonth = validMonth;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Credit() {
		super();
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLast4code() {
		return last4code;
	}

	public void setLast4code(String last4code) {
		this.last4code = last4code;
	}

	public int getPaymentDueDate() {
		return paymentDueDate;
	}

	public void setPaymentDueDate(int paymentDueDate) {
		this.paymentDueDate = paymentDueDate;
	}

	public int getStatementDay() {
		return statementDay;
	}

	public void setStatementDay(int statementDay) {
		this.statementDay = statementDay;
	}

	public int getRelative() {
		return relative;
	}

	public void setRelative(int relative) {
		this.relative = relative;
	}

	public Calendar getNextAlarmTime() {
		if (this.nextAlarmTime == null) {
			Calendar current = Calendar.getInstance();
			current.setTimeInMillis(System.currentTimeMillis());
			Calendar result = getAlarmCalendar(current);
			if (result.before(current)) {
				current.add(Calendar.MONTH, 1);
				result = getAlarmCalendar(current);
			}
			this.nextAlarmTime = result;
		}
		return nextAlarmTime;
	}

	private Calendar getAlarmCalendar(final Calendar relative) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(relative.getTimeInMillis());
		if (this.getRelative() == 1) {
			calendar.set(Calendar.DAY_OF_MONTH, this.getStatementDay());
			calendar.add(Calendar.DAY_OF_MONTH, this.getPaymentDueDate());
		} else {
			calendar.set(Calendar.DAY_OF_MONTH, this.getPaymentDueDate());
		}
		calendar.set(Calendar.HOUR_OF_DAY, 9);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar;
	}

	public Bank getBank() {
		return bank;
	}

	public void setBank(Bank bank) {
		this.bank = bank;
	}

	public void initValues(Cursor cursor) {
		setId(cursor.getLong(0));
		setName(cursor.getString(1));
		setLast4code(cursor.getString(3));
		setStatementDay(cursor.getInt(4));
		setPaymentDueDate(cursor.getInt(5));
		setRelative(cursor.getInt(6));
		setIcon(cursor.getString(7));
		setLimitRMB(cursor.getFloat(8));
		setLimitUSD(cursor.getFloat(9));
		setValidYear(cursor.getInt(10));
		setValidMonth(cursor.getInt(11));
		Bank bank = new Bank();
		bank.setId(cursor.getLong(12));
		bank.setName(cursor.getString(13));
		bank.setDefaultIcon(cursor.getString(14));
		bank.setHotline(cursor.getString(15));
		bank.setSmsNumber(cursor.getString(16));
		setBank(bank);
	}

	public CharSequence getPaymentText(Context context) {
		if (this.relative == 1) {
			return context.getResources().getString(
					R.string.relative_statementday)
					+ this.paymentDueDate
					+ context.getResources().getString(R.string.day2);
		} else {
			return context.getResources().getString(R.string.relative_absolute)
					+ this.paymentDueDate
					+ context.getResources().getString(R.string.day);
		}
	}

	public float getLimitRMB() {
		return limitRMB;
	}

	public void setLimitRMB(float limitRMB) {
		this.limitRMB = limitRMB;
	}

	public float getLimitUSD() {
		return limitUSD;
	}

	public void setLimitUSD(float limitUSD) {
		this.limitUSD = limitUSD;
	}

}
